clear
capture log close 
set more off 

/* Description:

  This file produces the US and Sweden combined tables for:
	*Table 1: Descriptive Statistics
	*Table 2: Exploring changes in Mother's IRP
	[*Table B6: Exploring changes in Father's IRP]
	*Figure 6: Decompositions of IRPs for Mothers' and Fathers' income
	*Figure 7: Decomposition of IRP for parent household income
	*Figure 8: IRP decompositions illustrating mother-daughter employment transmission

*/


*******************************************************
*TABLE 1: DESCRIPTIVE STATISTICS 
*******************************************************
use ${us_results}/descr_byperiod_us.dta, clear
gen country=2
append using ${swe_results}/descr_byperiod_SE.dta
replace country=1 if country==.
*Rescale employment statistics (to percent 1-100)
foreach t in 1 2 3 9 {
replace Mean`t'=100*Mean`t' if Variable=="Employment"
replace SD`t'=100*SD`t' if Variable=="Employment"
}
*Round numbers to nearest .1 
ds person Variable , not
local varstoround `r(varlist)'
foreach var in `varstoround' {
	replace `var'=round(`var',.1)
}
*Sort variables in order to present in table
gen sortv=.
replace sortv=1 if Variable=="Income"
//replace sortv=2 if Variable=="LogIncome"
replace sortv=3 if Variable=="AgeIncome"
replace sortv=4 if Variable=="Education"
replace sortv=5 if Variable=="Employment"
replace sortv=6 if Variable=="BirthYear"
replace sortv=7 if Variable=="Nindiv"
replace sortv=8 if Variable=="Nobs"
sort country person sortv 
* Add spaces for proper display in table 
//replace Variable="Log income" 		if Variable=="LogIncome"
replace Variable="Age at income" 	if Variable=="AgeIncome"
replace Variable="Birth year" 		if Variable=="BirthYear"
replace Variable="Individuals"	if Variable=="Nindiv"
replace Variable="Observations" if Variable=="Nobs"
*Keep only periods 1 and 3
drop *2 *9
*Drop log income 
drop if Variable=="LogIncome"
*Reshape countries wide 
reshape wide Mean1 SD1 Mean3 SD3, i(person Variable sortv) j(country)
*Need to make table narrow to fit on slide, so put "person" in own line
preserve 
clear 
set obs 4
gen person=_n
gen Variable=""
replace Variable="Daughters" 	if person==1
replace Variable="Sons" 		if person==2
replace Variable="Mothers" 		if person==3
replace Variable="Fathers" 		if person==4
tempfile extraobs
	save	`extraobs'
restore 
append using `extraobs'
replace sortv=0 if inlist(Variable,"Daughters", "Sons", "Mothers", "Fathers")	
sort person sortv
*Label variables 
foreach var of varlist Mean* {
	label var `var' "Mean"
}
foreach var of varlist SD* {
	label var `var' "SD"
}
*header lines and formatting
local headerfmt "& \multicolumn{4}{c}{Sweden} & \multicolumn{4}{c}{US} " "\cmidrule(lr){2-5}  \cmidrule(lr){6-9}" "& \multicolumn{2}{c}{1985-95} & \multicolumn{2}{c}{2008-19} & \multicolumn{2}{c}{1985-95} & \multicolumn{2}{c}{2008-19}" "\cmidrule(lr){2-3}  \cmidrule(lr){4-5} \cmidrule(lr){6-7}  \cmidrule(lr){8-9}" 
*Only use periods 1 and 3 (early/late for this table)
texsave Variable Mean11 SD11 Mean31 SD31 Mean12 SD12 Mean32 SD32 using ${tabfig}/table1.tex, align(l r r r r r r r r r) replace varlabels hlines(-2 -8 -10 -16 -18 -24 -26) nofix title("Descriptive statistics") headerlines("`headerfmt'")

clear





*******************************************************
*TABLE 2: EXPLORING CHANGES IN MOTHER'S IRP 
*******************************************************

*Causality vs. Measurement error

*** OLS results
use ${swe_results}/SE_irps.dta , clear
drop regnum se R N 
reshape wide irp, i(spec mother) j(period)
rename (irp1 irp2 ) (SE_irp1 SE_irp2 )
tempfile SEols 
  save `SEols'  
use ${us_results}/US_irps.dta , clear
drop regnum se R N
reshape wide irp, i(spec mother) j(period)
rename (irp1 irp2 ) (US_irp1 US_irp2 )
tempfile USols 
  save `USols'
use `SEols'
merge 1:1 mother specif using `USols', nogen
keep if specif==1 // only need baseline
gsort -mother specif 
gen approach=1
tempfile OLS 
  save `OLS'

*** IV using annual incomes  	
use  ${swe_results}/SE_irps_iv_v2.dta , clear 
drop if agez==0 // OLS estimates
rename (B1 B2 spec) (SE_irp1 SE_irp2 specif)
drop H? N?
tempfile SEivInc
	save `SEivInc'
use  ${us_results}/US_irps_iv_v2.dta , clear 
drop if agez==0 // OLS estimates
rename (B1 B2 spec) (US_irp1 US_irp2 specif)
drop H? N?
tempfile USivInc
	save `USivInc'
use `SEivInc'
merge 1:1 parent specif agez using `USivInc', nogen
gen mother=0 if parent=="f"
 replace mother=1 if parent=="m"
 drop parent 
label define iv1_vals 1 "Income" 2 "Income and HC" 3 "Both parents income, HC"
 label values specif iv1_vals
order mother specif agez, first
gsort -mother specif agez 
gen approach=2
tempfile IVInc
	save `IVInc'

*** IV using education, employment -- using Father's returns
use  ${swe_results}/SE_irps_eduempIV.dta , clear 
drop if specif==1 // OLS estimates
keep period mother specif sirp 
rename sirp SE_irp
reshape wide SE_irp, i(mother specif) j(period)
label define iv3_vals  2 "IV, Education (F returns)" 3 "IV, Education and employment (F returns)" 4 "IV, Educ and employ (cond)"
label values specif iv3_vals
tempfile SEivEdEmplF
	save `SEivEdEmplF'
use  ${us_results}/US_irps_eduempIV.dta , clear 
drop if specif==1 // OLS estimates
keep period mother specif sirp 
rename sirp US_irp
reshape wide US_irp, i(mother specif) j(period)
label define iv3_vals  2 "IV, Education (F returns)" 3 "IV, Education and employment (F returns)" 4 "IV, Educ and employ (cond)"
label values specif iv3_vals
tempfile USivEdEmplF
	save `USivEdEmplF'
use `SEivEdEmplF', clear
merge 1:1 specif mother using `USivEdEmplF', nogen 
drop if specif==2
gen approach=4
tempfile IVEdEmplF
save `IVEdEmplF'

*** Combine all 
use `OLS', clear 
append using `IVInc'
append using `IVEdEmplF'

foreach var in SE_irp1 SE_irp2  US_irp1 US_irp2  {
	replace `var'=round(`var',.001)
}
order mother approach specif agez, first
gsort -mother approach specif agez
gen SE_diff=SE_irp2-SE_irp1
gen US_diff=US_irp2-US_irp1
*header lines and formatting
local headerfmt "Approach & Specification & \multicolumn{2}{c}{Sweden} & \multicolumn{2}{c}{US} " "\cmidrule(lr){3-5}  \cmidrule(lr){6-8}" " & & 1985-95 & 2008-19 & Change & 1985-95 & 2008-19 & Change"  
gen approach_descr=""
replace approach_descr="OLS (baseline)" if approach==1 & specif==1
replace approach_descr="IV: income, child age 12" if approach==2 & agez==12
replace approach_descr="IV: income, child age 21" if approach==2 & agez==21
replace approach_descr="IV: educ, employ" if approach==4 & specif==3
 replace specif=1 if approach_descr=="IV: educ, employ" & approach==4 // need to adjust specif value to match other IV, OLS for table
replace approach_descr="IV: educ, employ (cond)" if approach==4 & specif==4
*** Create tables
*TABLE 2: EXPLORING CHANGES IN MOTHER'S IRP 
preserve 
keep if mother==1
gsort -mother approach specif agez
texsave approach_descr specif SE_irp1 SE_irp2 SE_diff US_irp1 US_irp2 US_diff using ${tabfig}/table2.tex, align(l l  r r r r r r) replace title("Exploring changes in Mother's IRP") hlines(-2 -4 -6 -8) //headerlines("`headerfmt'") 
restore 
*TABLE B6: EXPLORING CHANGES IN FATHER'S IRP 
preserve 
keep if mother==0
gsort -mother approach specif agez
texsave approach_descr specif SE_irp1 SE_irp2 SE_diff US_irp1 US_irp2 US_diff using ${tabfig}/tableB6.tex, align(l l  r r r r r r ) replace title("Exploring changes in Father's IRP") hlines(-2 -4 -6 -8) //headerlines("`headerfmt'") 
restore 







	
************************************************************************
*** FIGURE 6: DECOMPOSITIONS OF IRPS FOR MOTHERS' AND FATHERS' INCOME	
************************************************************************
local yrmin1=1985 
local yrmax1=1995
local yrmin3=2008
local yrmax3=2018
clear
append using ${us_results}/model_5b_sequential_`yrmin1'_`yrmax1'.dta
append using ${us_results}/model_5b_sequential_`yrmin3'_`yrmax3'.dta
append using ${us_results}/model_5bF_sequential_`yrmin1'_`yrmax1'.dta
append using ${us_results}/model_5bF_sequential_`yrmin3'_`yrmax3'.dta
gen country=2
gen country_name="US"
local yrmax3=2019
append using ${swe_results}/SE_model_5b_sequential_`yrmin1'_`yrmax1'.dta
append using ${swe_results}/SE_model_5b_sequential_`yrmin3'_`yrmax3'.dta
append using ${swe_results}/SE_model_5bF_seq_`yrmin1'_`yrmax1'.dta
append using ${swe_results}/SE_model_5bF_seq_`yrmin3'_`yrmax3'.dta
replace country=1 if country==.
replace country_name="Sweden" if country_name==""
**** Decomposition  
*Decomposition elements specific to this model 
g BH=((pi1*O_p)+pi2)*O_p*(vHp/vYp)	// own HC										-->	 1
g BE=pi1*(O_0p^2)*(vEta_ep/vYp)		// own (non-HC) employment						-->	 2
g BU=pi1*(vEta_p/vYp)					// own residual yp								-->	 3
g b_HpHsp=O_p*d1*((O_s*pi1s)+pi2s)*(vHp/vYp) 	// sorting on HC 						-->  4
g b_EpyEspy=pi1s*(CovEpyEspy/vYp)				// sorting on residual income 			-->	 5A
g b_HpEspy=(O_p*pi1s*CovHpEspy/vYp) 			// sorting: own HC, spouse resid		-->	 5B (PART OF IT)
g b_EpyHsp=((pi1s*O_s)+pi2s)*(CovHspEpy/vYp) 	// sorting: spouse HC, own resid		-->	 5B (PART OF IT)
g b_sp=pi1*(O_0p^2)*(phi2^2)*(vEta_sp/vYp) 	// part of spouse HC orthogonal to own 		-->  5C
*Break down Hp channel into part related to Employment and part orthogonal to it
g BHinclE=(((O_0p*(phi1+(phi2*d1)))*((pi1*(O_0p*(phi1+(phi2*d1))))+pi2)) + (2*pi1*O_1p*(O_0p*(phi1+(phi2*d1)))))*(vHp/vYp)
g BHexclE=(O_1p*((pi1*O_1p)+pi2))*(vHp/vYp)
* Aggregate spouse/AM component 
g B_ownchar=BH+BE+BU
g B_spousesort=b_sp+b_HpHsp+b_HpEspy+b_EpyHsp+b_EpyEspy
*Break down AM
g b_4=b_HpHsp // sorting on HC
g b_5=b_EpyEspy+b_HpEspy+b_EpyHsp+b_sp // other sorting
* Time period 
gen period=.
replace period=1 if yearmin==`yrmin1'
replace period=3 if yearmin==`yrmin3'
label define periodvals 1 "1985-1995" 3 "2008-2019"
label values period periodvals
*5b - Mothers - with detailed HC
graph bar b_4 b_5 BHexclE BHinclE BE BU  if modelnum=="5b", over(country_name) over(period) stack ///
	bar(1, color(emidblue%50)) bar(2, color(emidblue%20)) ///
	bar(3, color(red%30)) bar(4, color(red%80)) ///
	bar(6, color(maroon%70)) bar(5, color(cranberry%100)) ///
	ytitle("IRP") graphr(fc(white) c(white))  /*ysc(r(0 0.15)) */ ylab( , nogrid) yline(0, lcolor(gs10)) legend(rows(2) order(3 5 1 4 6 2) label(1 "AM on HC") label(2 "Other AM") label(3 "Human capital (HC)") label(4 "HC-Employment sorting") label(5 "Empoyment") label(6 "Residual Inc") region(lstyle(none))) graphr(fc(white) c(white))  saving(${tabfig}/fig6a, replace)	
graph export ${tabfig}/fig6a.pdf, replace	
graph export ${tabfig}/fig6a.eps, replace	
*5b - Fathers - with detailed HC
graph bar b_4 b_5 BHexclE BHinclE BE BU if modelnum=="5bF", over(country_name) over(period) stack ///
	bar(1, color(emidblue%50)) bar(2, color(emidblue%20)) ///
	bar(3, color(ltblue%40)) bar(4, color(eltblue%70)) ///
	bar(5, color(navy)) bar(6, color(ebblue)) ///
	ytitle("IRP") graphr(fc(white) c(white))  /*ysc(r(0 0.35)) */ ylab( , nogrid) yline(0, lcolor(gs10)) legend(rows(2) order(3 5 1 4 6 2) label(1 "AM on HC") label(2 "Other AM") label(3 "Human capital (HC)") label(4 "HC-Employment sorting") label(5 "Empoyment") label(6 "Residual Inc") region(lstyle(none))) graphr(fc(white) c(white)) 	saving(${tabfig}/fig6b, replace)	
graph export ${tabfig}/fig6b.pdf, replace
graph export ${tabfig}/fig6b.eps, replace


************************************************************************
*** FIGURE 7: DECOMPOSITION OF IRP FOR PARENT HOUSEHOLD INCOME
************************************************************************
local yrmin1=1985 
local yrmax1=1995
local yrmin3=2008
local yrmax3=2018
clear
append using ${us_results}/model_5bavg_sequential_`yrmin1'_`yrmax1'.dta
append using ${us_results}/model_5bavg_sequential_`yrmin3'_`yrmax3'.dta
gen country=2
gen country_name="US"
local yrmax3=2019
append using ${swe_results}/SE_model_5bavg_seq_`yrmin1'_`yrmax1'.dta
append using ${swe_results}/SE_model_5bavg_seq_`yrmin3'_`yrmax3'.dta
replace country=1 if country==.
replace country_name="Sweden" if country_name==""
* Time period 
gen period=.
replace period=1 if yearmin==`yrmin1'
replace period=3 if yearmin==`yrmin3'
label define periodvals 1 "1985-1995" 3 "2008-2019"
label values period periodvals
**** Decomposition  
*Decomposition elements specific to this model 
g b_Hp=.5*((pi1*O_p)+pi2)*O_p*(vHp/vYp)	// own HC (mother)
g b_ep=.5*pi1*(O_0p^2)*(vEta_ep/vYp)		// own (non-HC) employment
g b_p=.5*pi1*(vEta_p/vYp)					// own residual yp
g b_Hsp=.5*((pi1s*O_s)+pi2s)*O_s*(vHsp/vYp) // spouse HC (father)
g b_es=.5*pi1s*(O_0s^2)*(vEta_es/vYp)		// spouse (non-HC) employment
g b_s=.5*pi1s*(vEta_s/vYp)					// spouse residual ys
g b_sp=.5*pi1*(O_0p^2)*(phi2^2)*(vEta_sp/vYp) 	// part of spouse HC orthogonal to own HC -- PUT THIS WITH NON-HC AM
g b_ss=.5*pi1s*(O_0s^2)*(phi2s^2)*(vEta_ss/vYp) 	// part of own HC orthogonal to spouse HC -- PUT THIS WITH NON-HC AM
g b_HpHsp=.5*(O_p*d1*((O_s*pi1s)+pi2s)*(vHp/vYp)+((pi1*O_p)+pi2)*O_s*d1s*(vHsp/vYp)) 	// sorting on HC
g b_HpEspy=.5*((O_p*pi1s*CovHpEspy/vYp)+(((O_p*pi1)+pi2)*CovHpEspy/vYp)) 			// sorting: own HC, spouse resid
g b_EpyHsp=.5*(((pi1s*O_s)+pi2s)*(CovHspEpy/vYp)+(pi1*O_s)*(CovHspEpy/vYp)) 	// sorting: spouse HC, own resid
g b_EpyEspy=.5*(pi1s*(CovEpyEspy/vYp)+(pi1*CovEpyEspy/vYp))				// sorting on residual income
*Mother char / Father char / Assort Mating
g b_Moth= (b_Hp+b_ep+b_p)
g b_Fath=b_Hsp+b_es+b_s 
g b_AM=b_sp+b_ss+b_HpHsp+b_HpEspy+b_EpyHsp+b_EpyEspy
g b_AMhc=b_HpHsp
g b_AMeta=b_sp+b_ss+b_HpEspy+b_EpyHsp+b_EpyEspy
*5b - average parent income
graph bar b_AMhc b_AMeta b_Fath b_Moth if modelnum=="5bavg", over(country_name) over(period) stack ///
	bar(1, color(emidblue%50)) bar(2, color(emidblue%20)) ///
	bar(3, color(navy)) bar(4, color(maroon))  ///
	ytitle("IRP") graphr(fc(white) c(white))  /*ysc(r(0 0.35)) */ ylab( , nogrid) yline(0, lcolor(gs10)) legend(rows(2) order( 3 1 4 2) label(1 "AM on HC") label(2 "Other AM") label(3 "Fathers") label(4 "Mothers") region(lstyle(none))) graphr(fc(white) c(white)) 	saving(${tabfig}/fig7, replace)	
graph export ${tabfig}/fig7.pdf, replace
graph export ${tabfig}/fig7.eps, replace










************************************************************************
*** FIGURE 8
************************************************************************

***FIGURE 8B - US
clear
local yrmin1=1985 
local yrmax1=1995
local yrmin3=2008
local yrmax3=2018
append using ${us_results}/model_5b_dau_empu_`yrmin1'_`yrmax1'.dta
append using ${us_results}/model_5b_dau_empu_`yrmin3'_`yrmax3'.dta
append using ${us_results}/model_5b_dau_empxb_`yrmin1'_`yrmax1'.dta
append using ${us_results}/model_5b_dau_empxb_`yrmin3'_`yrmax3'.dta
replace modelnum=model_descr // so that rest of code will use empu and empxb
drop model_descr
sort modelnum
gen period=.
replace period=1 if yearmin==`yrmin1' & yearmax==`yrmax1'
replace period=3 if yearmin==`yrmin3' & yearmax==`yrmax3'
label define periodvals 1 "`yrmin1'-`yrmax1'"  3 "`yrmin3'-`yrmax3'", replace
label values period periodvals 
** Decomposition  
*Decomposition elements specific to this model 
g BH=((pi1*O_p)+pi2)*O_p*(vHp/vYp)	// own HC										-->	 1
g BE=pi1*(O_0p^2)*(vEta_ep/vYp)		// own (non-HC) employment						-->	 2
g BU=pi1*(vEta_p/vYp)					// own residual yp								-->	 3
g b_HpHsp=O_p*d1*((O_s*pi1s)+pi2s)*(vHp/vYp) 	// sorting on HC 						-->  4
g b_EpyEspy=pi1s*(CovEpyEspy/vYp)				// sorting on residual income 			-->	 5A
g b_HpEspy=(O_p*pi1s*CovHpEspy/vYp) 			// sorting: own HC, spouse resid		-->	 5B (PART OF IT)
g b_EpyHsp=((pi1s*O_s)+pi2s)*(CovHspEpy/vYp) 	// sorting: spouse HC, own resid		-->	 5B (PART OF IT)
g b_sp=pi1*(O_0p^2)*(phi2^2)*(vEta_sp/vYp) 	// part of spouse HC orthogonal to own 		-->  5C
*Break down Hp channel into part related to Employment and part orthogonal to it
g BHinclE=(((O_0p*(phi1+(phi2*d1)))*((pi1*(O_0p*(phi1+(phi2*d1))))+pi2)) + (2*pi1*O_1p*(O_0p*(phi1+(phi2*d1)))))*(vHp/vYp)
g BHexclE=(O_1p*((pi1*O_1p)+pi2))*(vHp/vYp)
g pctHinclE=100*BHinclE/(BH+BE+BU)
g pctHexclE=100*BHexclE/(BH+BE+BU)
	g temp=BHinclE+BHexclE // verify sums to BH
	tab BH temp 
	drop temp
* Aggregate spouse/AM component 
g B_ownchar=BH+BE+BU
g B_spousesort=b_sp+b_HpHsp+b_HpEspy+b_EpyHsp+b_EpyEspy
g pct_ownchar=100*B_ownchar/B
g pct_spousesort=100*B_spousesort/B
*Break down AM
g b_4=b_HpHsp // sorting on HC
g b_5=b_EpyEspy+b_HpEspy+b_EpyHsp+b_sp // other sorting
*Compute percent of Parent Char due to H, E, U. Again, same as simpler model without AM (divide by BH+BE+BU rather than B, so percent of own char).
g pctH=100*BH/(BH+BE+BU)
g pctE=100*BE/(BH+BE+BU)
g pctU=100*BU/(BH+BE+BU)
gen modelnm=""
replace modelnm="Empl" if modelnum=="empxb"
replace modelnm="Non-Empl" if modelnum=="empu"
*Figure 8b (US): IRP decompositions illustrating mother-daughter employment transmission
graph bar b_4 b_5 BHexclE BHinclE BE BU  , over(modelnm) over(period) stack ///
	bar(1, color(emidblue%50)) bar(2, color(emidblue%20)) ///
	bar(3, color(red%30)) bar(4, color(red%80)) ///
	bar(6, color(maroon%70)) bar(5, color(cranberry%100)) ///
	ytitle("IRP") graphr(fc(white) c(white))  /*ysc(r(0 0.15)) */ ylab( , nogrid) yline(0, lcolor(gs10)) legend(rows(2) order(3 5 1 4 6 2) label(1 "AM on HC") label(2 "Other AM") label(3 "Human capital (HC)") label(4 "HC-Employment sorting") label(5 "Empoyment") label(6 "Residual Inc") region(lstyle(none))) graphr(fc(white) c(white))  saving(${tabfig}/fig8b, replace)	
graph export ${tabfig}/fig8b.pdf, replace
graph export ${tabfig}/fig8b.eps, replace


****FIGURE 8A - SWEDEN
local yrmin1=1985 
local yrmax1=1995
local yrmin3=2008
local yrmax3=2019
clear
append using ${swe_results}/model_5b_dau_empu_`yrmin1'_`yrmax1'.dta
append using ${swe_results}/model_5b_dau_empu_`yrmin3'_`yrmax3'.dta
append using ${swe_results}/model_5b_dau_empxb_`yrmin1'_`yrmax1'.dta
append using ${swe_results}/model_5b_dau_empxb_`yrmin3'_`yrmax3'.dta
replace modelnum=model_descr // so that rest of code will use empu and empxb
drop model_descr
sort modelnum
gen period=.
replace period=1 if yearmin==`yrmin1' & yearmax==`yrmax1'
replace period=3 if yearmin==`yrmin3' & yearmax==`yrmax3'
label define periodvals 1 "`yrmin1'-`yrmax1'"  3 "`yrmin3'-`yrmax3'", replace
label values period periodvals 
** Decomposition  
*Decomposition elements specific to this model 
g BH=((pi1*O_p)+pi2)*O_p*(vHp/vYp)	// own HC										-->	 1
g BE=pi1*(O_0p^2)*(vEta_ep/vYp)		// own (non-HC) employment						-->	 2
g BU=pi1*(vEta_p/vYp)					// own residual yp								-->	 3
g b_HpHsp=O_p*d1*((O_s*pi1s)+pi2s)*(vHp/vYp) 	// sorting on HC 						-->  4
g b_EpyEspy=pi1s*(CovEpyEspy/vYp)				// sorting on residual income 			-->	 5A
g b_HpEspy=(O_p*pi1s*CovHpEspy/vYp) 			// sorting: own HC, spouse resid		-->	 5B (PART OF IT)
g b_EpyHsp=((pi1s*O_s)+pi2s)*(CovHspEpy/vYp) 	// sorting: spouse HC, own resid		-->	 5B (PART OF IT)
g b_sp=pi1*(O_0p^2)*(phi2^2)*(vEta_sp/vYp) 	// part of spouse HC orthogonal to own 		-->  5C
*Break down Hp channel into part related to Employment and part orthogonal to it
g BHinclE=(((O_0p*(phi1+(phi2*d1)))*((pi1*(O_0p*(phi1+(phi2*d1))))+pi2)) + (2*pi1*O_1p*(O_0p*(phi1+(phi2*d1)))))*(vHp/vYp)
g BHexclE=(O_1p*((pi1*O_1p)+pi2))*(vHp/vYp)
g pctHinclE=100*BHinclE/(BH+BE+BU)
g pctHexclE=100*BHexclE/(BH+BE+BU)
	g temp=BHinclE+BHexclE // verify sums to BH
	tab BH temp 
	drop temp
* Aggregate spouse/AM component 
g B_ownchar=BH+BE+BU
g B_spousesort=b_sp+b_HpHsp+b_HpEspy+b_EpyHsp+b_EpyEspy
g pct_ownchar=100*B_ownchar/B
g pct_spousesort=100*B_spousesort/B
*Break down AM
g b_4=b_HpHsp // sorting on HC
g b_5=b_EpyEspy+b_HpEspy+b_EpyHsp+b_sp // other sorting
*Compute percent of Parent Char due to H, E, U. Again, same as simpler model without AM (divide by BH+BE+BU rather than B, so percent of own char).
g pctH=100*BH/(BH+BE+BU)
g pctE=100*BE/(BH+BE+BU)
g pctU=100*BU/(BH+BE+BU)
gen modelnm=""
replace modelnm="Empl" if modelnum=="empxb"
replace modelnm="Non-Empl" if modelnum=="empu"
*TABLE 8a
graph bar b_4 b_5 BHexclE BHinclE BE BU  , over(modelnm) over(period) stack ///
	bar(1, color(emidblue%50)) bar(2, color(emidblue%20)) ///
	bar(3, color(red%30)) bar(4, color(red%80)) ///
	bar(6, color(maroon%70)) bar(5, color(cranberry%100)) ///
	ytitle("IRP") graphr(fc(white) c(white))  /*ysc(r(0 0.15)) */ ylab( , nogrid) yline(0, lcolor(gs10)) legend(rows(2) order(3 5 1 4 6 2) label(1 "AM on HC") label(2 "Other AM") label(3 "Human capital (HC)") label(4 "HC-Employment sorting") label(5 "Empoyment") label(6 "Residual Inc") region(lstyle(none))) graphr(fc(white) c(white))  saving(${tabfig}/fig8a, replace)	
graph export ${tabfig}/fig8a.pdf, replace
graph export ${tabfig}/fig8a.eps, replace




	
	

clear








